//
//  JCProgressView.m
//  SquashSpark
//
//  Created by 郭吉成 on 2017/11/30.
//  Copyright © 2017年 Coollang-YF. All rights reserved.
//

#import "JCProgressView.h"

@interface JCProgressView ()

@property (nonatomic, strong) CAShapeLayer *trackLayer;
@property (nonatomic, strong) UIBezierPath *trackPath;

@property (nonatomic, strong) CAShapeLayer *track2Layer;
@property (nonatomic, strong) UIBezierPath *track2Path;

@property (nonatomic, strong) CAShapeLayer *progressLayer;
@property (nonatomic, strong) UIBezierPath *progressPath;

@property (nonatomic, strong) CAShapeLayer *lastProgressLayer;
@property (nonatomic, strong) UIBezierPath *lastProgressPath;
@property (nonatomic, assign) CGFloat lastProgress;

@property (nonatomic, strong) UILabel *valueLabel;
@property (nonatomic, strong) UILabel *subscriptLabel;
@property (nonatomic, strong) UIImageView *imageView;

@end

@implementation JCProgressView

- (void)drawRect:(CGRect)rect{
    [super drawRect:rect];
    [self initUI];
}

/**
 控件初始化
 */
- (void)initUI{
    _trackLayer = [CAShapeLayer new];
    _trackLayer.fillColor = nil;
    _trackLayer.frame = self.bounds;
    [self.layer addSublayer:_trackLayer];
    
//    _track2Layer = [CAShapeLayer new];
//    _track2Layer.fillColor = nil;
//    _track2Layer.frame = self.bounds;
//    [self.layer addSublayer:_track2Layer];
    
    _progressLayer = [CAShapeLayer new];
    [self.layer addSublayer:_progressLayer];
    _progressLayer.fillColor = nil;
    _progressLayer.lineCap = kCALineCapSquare;
    _progressLayer.frame = self.bounds;
    
    _lastProgressLayer = [CAShapeLayer new];
    [self.layer addSublayer:_lastProgressLayer];
    _lastProgressLayer.fillColor = nil;
    _lastProgressLayer.lineCap = kCALineCapSquare;
    _lastProgressLayer.frame = self.bounds;
    
    _valueLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.bounds), CGRectGetHeight(self.bounds))];
    _valueLabel.textColor = [UIColor colorWithRed:235/256.0 green:133/256.0 blue:51/256.0 alpha:1.0];
    _valueLabel.textAlignment = NSTextAlignmentCenter;
    _valueLabel.font = kOpenSansFont(OpenSansRegular, CGRectGetHeight(self.bounds) / 6.0);
    [self addSubview:_valueLabel];
    
    //默认6
    self.progressWidth = 6;
    
    
//    _subscriptLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(_valueLabel.frame)+14, CGRectGetWidth(self.bounds), 11)];
//    _subscriptLabel.font = [UIFont systemFontOfSize:10];
//    _subscriptLabel.textColor = [[UIColor whiteColor] colorWithAlphaComponent:0.5];
//    _subscriptLabel.textAlignment = NSTextAlignmentCenter;
//    //    _subscriptLabel.text = @"当前电量";
//    [self addSubview:_subscriptLabel];
    
//    _imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 0.3*self.bounds.size.width, 0.3*self.bounds.size.height)];
//    _imageView.center = CGPointMake(CGRectGetWidth(self.bounds) / 2, CGRectGetHeight(self.bounds) / 2);
//    [self addSubview:_imageView];
    
}

#pragma mark - 底部圆圈
/**
 绘制底部圆圈
 */
- (void)setTrack
{
    
    //bezierPathWithOvalInRect
    _trackPath = [UIBezierPath bezierPathWithArcCenter:CGPointMake(CGRectGetWidth(self.bounds) / 2, CGRectGetHeight(self.bounds) / 2)
                                                radius:(self.bounds.size.width)/ 2.0 - 0.5*_progressWidth
                                            startAngle:0
                                              endAngle:M_PI * 2
                                             clockwise:YES];;
    _trackLayer.path = _trackPath.CGPath;
    
//    _track2Path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(CGRectGetWidth(self.bounds) / 2, CGRectGetHeight(self.bounds) / 2) radius:(self.bounds.size.width)/ 2 - _progressWidth startAngle:0 endAngle:M_PI * 2 clockwise:YES];;
//    _track2Layer.path = _track2Path.CGPath;
}

/**
 绘制底部圆圈颜色
 */
- (void)setTrackColor:(UIColor *)trackColor
{
    _trackLayer.strokeColor = [trackColor colorWithAlphaComponent:0.2].CGColor;
}

#pragma mark - 进度弧
/**
 绘制进度圆弧
 */
- (void)setProgress
{
    
    _progressPath = [UIBezierPath bezierPathWithArcCenter:CGPointMake(CGRectGetWidth(self.bounds) / 2.0, CGRectGetHeight(self.bounds) / 2.0)
                                                   radius:(self.bounds.size.width)/ 2.0 - 0.5*_progressWidth
                                               startAngle:(M_PI * 2) * _lastProgress - M_PI_2
                                                 endAngle:(M_PI * 2) * _progress - M_PI_2
                                                clockwise:YES];
    _progressLayer.path = _progressPath.CGPath;
    _lastProgressPath = [UIBezierPath bezierPathWithArcCenter:CGPointMake(CGRectGetWidth(self.bounds) / 2.0, CGRectGetHeight(self.bounds) / 2.0)
                                                       radius:(self.bounds.size.width)/ 2.0 - _progressWidth
                                                   startAngle:-M_PI_2
                                                     endAngle:(M_PI * 2) * _lastProgress - M_PI_2
                                                    clockwise:YES];
    _lastProgressLayer.path = _lastProgressPath.CGPath;
    _valueLabel.text = [NSString stringWithFormat:@"%.0f%%",_progress*100];
}

/**
 设置进度条宽度
 */
- (void)setProgressWidth:(float)progressWidth
{
    _progressWidth = progressWidth;
    _trackLayer.lineWidth = _progressWidth;
    _progressLayer.lineWidth = _progressWidth;
    _lastProgressLayer.lineWidth = _progressWidth;
    _track2Layer.lineWidth = 2;
    
    [self setTrack];
    [self setProgress];
}

/**
 设置进度条颜色
 */
- (void)setProgressColor:(UIColor *)progressColor
{
    _progressLayer.strokeColor = progressColor.CGColor;
    _lastProgressLayer.strokeColor = progressColor.CGColor;
    _valueLabel.textColor = progressColor;
    _track2Layer.strokeColor = [progressColor colorWithAlphaComponent:0.2].CGColor;
}

/**
 设置进度百分比
 */
- (void)setProgress:(float)progress{
    _imageView.hidden = YES;
    if (progress > 1) {
        return;
    }
    _progress = progress;
    
    [self setProgress];
    if (progress == 1) {
//        [self showSuccessful];
    }else{
        _imageView.hidden = YES;
    }
}

/**
 设置进度百分比带动画
 */
- (void)setProgress:(float)progress animated:(BOOL)animated{
    [self setProgress:progress];
    _lastProgress = _progress;
    // 给这个layer添加动画效果
    if (!animated) {
        return;
    }
    CABasicAnimation *pathAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
    
    pathAnimation.duration = 0.8;
    
    pathAnimation.fromValue = [NSNumber numberWithFloat:0.0f];
    
    pathAnimation.toValue = [NSNumber numberWithFloat:1.0f];
    
    [_progressLayer addAnimation:pathAnimation forKey:nil];
}

- (void)showSuccessful{
    
    _valueLabel.text = @"";
    _imageView.hidden = NO;
    _imageView.image = [UIImage imageNamed:@"对号.png"];
}

- (void)showFailed{
    
    _valueLabel.text = @"";
    _imageView.hidden = NO;
    _imageView.image = [UIImage imageNamed:@"错号.png"];
}
@end
